﻿using System;
using System.Windows.Forms;
using ASRSubsDownloader.Common;

namespace ASRSubsDownloader.Forms
{
    public partial class ConfigForm : Form
    {
        public ConfigForm()
        {
            InitializeComponent();
            OnLoad(null, new EventArgs());
        }

        private void btnShowDialog_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowNewFolderButton = true;
            folderBrowserDialog1.ShowDialog();
            labelPath.Text = folderBrowserDialog1.SelectedPath;
        }

        protected void OnLoad(object sender, EventArgs e)
        {
            cbLanguages.DataSource = Config.Languages;
            cbLanguages.DisplayMember = "Name";
            cbLanguages.ValueMember = "Code";
            if (Config.Language != null)
                cbLanguages.SelectedValue = Config.Language;
            chkAutoStart.Checked = Config.AutoRun;
            labelPath.Text = Config.BaseFolder;
            txtExtension.Text = string.IsNullOrEmpty(Config._fileExtensions) ? "avi,mkv,mp4,wmv" : Config._fileExtensions;
        }

        public void Show(string Message)
        {
            this.OnLoad(null, new EventArgs());
            MessageBox.Show(Message, "Atenção");
            this.ShowDialog();

        }

        public void ShowForm()
        {
            this.OnLoad(null, new EventArgs());
            this.Show();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if(labelPath.Text == "" || cbLanguages.SelectedValue == "" || txtExtension.Text == "")
            {
                MessageBox.Show("Preencha o idioma e escolha o caminho da pasta para continuar!");
                return;
            }
            Config.BaseFolder = labelPath.Text;
            Config.Language = cbLanguages.SelectedValue.ToString();
            Config.FileExtensions = txtExtension.Text.Split(',');
            Config.AutoRun = chkAutoStart.Checked;
            Config.SaveConfig();
            

            this.Hide();
            this.Dispose();
        }
    }
}
